from my_logger import logger


def decimal_to_binary(num):
    result = ''
    if num == 0:
        return '0000'
    while num > 0:
        result = str(num % 2) + result
        num //= 2
    while len(result) != 4:
        result = "0" + result

    return result[::-1]


def get_word(w: str):
    r = ""
    i = 0
    while i < len(w):
        r = decimal_to_binary(int(w[i:i + 1],16)) + " " + r
        i += 1
    return r


word_mapping = [["温度正常", "温度异常"], ["E2正常", "E2 异常"], ["时钟正常", "时钟异常"], ["校表正常", "校表异常"],
                ["功率正常", "功率过载"], ["时钟正常", "维电器异"], ["合闸", "合闸时无功率"], ["红外正常", "红外异常"],

                ["合闸", "跳闸"], ["关机", "空调开机"], ["解除保电", "保电"], ["", "节假日"],
                ["", "透支"], ["", "金额报警"], ["", "开盖"], ["", ":编程按键打开"],

                ["", "当前温度不允许开空调"], ["", "当前时段不允许开(温控，时段限制)"], ["温控使能", "温控失能"], ["当前为制冷模式", ":当前为制暖模式"],
                ["", ""], ["", ""], ["", ""], ["正常工作模书", "生产调试模式"],
                ]


def get_run_word(w):
    p = get_word(w).replace(" ", "")
    index = 0
    v = ""
    for i in p:
        if i=="1":
            v += word_mapping[index][int(i)] + " "
        index += 1
    logger.info(v+"---------")
    print(v)
    return p[9] ,p[19],v


def test():
    # 温控失能，金额报警，透支，保电，开机
    p = get_word("043600")
    print(p)
    index = 0
    for i in p:
        if i == " ":
            continue
        print(word_mapping[index][int(i)])
        index += 1


print(get_run_word("0c0280"))
